package com.hmjk.health.views;

import java.math.RoundingMode;
import java.text.DecimalFormat;

import com.hmjk.health.R;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

public class GluCakyLayout extends LinearLayout{

	private Context mContext;
	private GluCakyView gluCaky;
	private TextView lowTxt,normal0Txt,normal1Txt,high0Txt,high1Txt,high2Txt,totalTxt;
	private TextView txtArr[] = new TextView[6];
	private int resArr[] ={R.string.bp_measure_normal,
			               R.string.bp_measure_normal_high,
			               R.string.bp_measure_hbp_mild,	
			               R.string.bp_measure_hbp_moderate,
			               R.string.bp_measure_hbp_severe,	
			               R.string.bp_measure_lbp,
	                      };

    private int total = 0;    
    
    private int values[]= {0,0,0,0,0,0};
    private float percent[] = {0.0f,0.0f,0.0f,0.0f,0.0f,0.0f};
	
	private void initViews()
	{		
		LayoutInflater inflater=(LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		inflater.inflate(R.layout.glucaky_layout, this);
		gluCaky = (GluCakyView) findViewById(R.id.gluCaky);
		lowTxt = (TextView) findViewById(R.id.lowTxt);
		normal0Txt = (TextView) findViewById(R.id.normal0Txt);
		normal1Txt = (TextView) findViewById(R.id.normal1Txt);	
		high0Txt = (TextView) findViewById(R.id.high0Txt);
		high1Txt = (TextView) findViewById(R.id.high1Txt);
		high2Txt = (TextView) findViewById(R.id.high2Txt);			
		totalTxt = (TextView) findViewById(R.id.totalTxt);	
		
		String s = " " + getResources().getString(R.string.measure_null_value) + getResources().getString(R.string.measure_times_unit);
		lowTxt.setText(getResources().getString(R.string.bp_lvl_lbp) + s);
		normal0Txt.setText(getResources().getString(R.string.bp_lvl_normal) + s);
		normal1Txt.setText(getResources().getString(R.string.bp_lvl_normal_high) + s);
		high0Txt.setText(getResources().getString(R.string.bp_lvl_hbp_mild) + s);
		high1Txt.setText(getResources().getString(R.string.bp_lvl_hbp_moderate) + s);
		high2Txt.setText(getResources().getString(R.string.bp_lvl_hbp_severe) + s);
		s = getResources().getString(R.string.bp_measure_total, getResources().getString(R.string.measure_null_value));
		totalTxt.setText(s);
				
		txtArr[0] = normal0Txt;
		txtArr[1] = normal1Txt;
		txtArr[2] = high0Txt;
		txtArr[3] = high1Txt;
		txtArr[4] = high2Txt;
		txtArr[5] = lowTxt;
		
	}
	
	public GluCakyLayout(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		mContext = context;
		initViews();
	}	
	
	public GluCakyLayout(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		mContext = context;
		initViews();
	}	
	
	public GluCakyLayout(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		mContext = context;
		initViews();
	}


    public void setParameters(int... para)  //noraml,normal1,high0....low0;
    {
    	gluCaky.setParameters(para);
    	
		 total = 0;
		 int len = values.length<para.length?values.length:para.length;
		 for(int i = 0;i<values.length;i++)
		 {
			 values[i] = 0;
		 }
		 
		 for (int i = 0; i < len; i++) {
			 values[i] = para[i];
			 total += para[i];
		 } 
		 
		 for (int i = 0; i < len; i++) {
			 float p = (float)values[i]/(float)total;
	         p = (float)(Math.round(p*1000))/1000;
	         p = p*100;
			 percent[i] = p;
			 DecimalFormat decimalFormat=new DecimalFormat(".0");
			 decimalFormat.setRoundingMode(RoundingMode.HALF_UP);

			 String s = getResources().getString(resArr[i],Integer.toString(values[i]),decimalFormat.format(p));
			 txtArr[i].setText(s);
		 }	
		 
		 String s = getResources().getString(R.string.bp_measure_total,Integer.toString(total));
		 this.totalTxt.setText(s);
    }

	
}